package com.hulk.dryad.admin.initializer;

import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;


import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 *
 * ConditionalOnWebApplication 注解会导致 RespExcel 导出excel失败，返回乱码串
 *
 * @author lqx
 * @date 2019-06-24
 */
@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@RequiredArgsConstructor
public class WebMvcConfig implements WebMvcConfigurer {


	private final LocaleChangeInterceptor localeChangeInterceptor;


	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(localeChangeInterceptor).addPathPatterns("/**").excludePathPatterns("/rest/heartbeat").excludePathPatterns("/favicon.ico");
	}


	/**
	 *  解决中文乱码问题
	 */
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		WebMvcConfigurer.super.configureMessageConverters(converters);
		StringHttpMessageConverter converter = new StringHttpMessageConverter(
				StandardCharsets.UTF_8);
		converters.add(converter);
	}
	@Override
	public void addViewControllers(ViewControllerRegistry registry)
	{
		registry.addViewController("/").setViewName("/index.html");
	}
//	@Override
//	public void addResourceHandlers(ResourceHandlerRegistry registry) {
//		registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
//		registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
//	}

//	/**要完全禁用文件 extensions*/
//
//	@Override
//	public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
//		configurer.favorPathExtension(false);
//
//	}
//	@Override
//	public void configurePathMatch(PathMatchConfigurer configurer) {
//		configurer.setUseSuffixPatternMatch(false);
//	}


}
